Completed
Push — master ( a31174...af3b49 )
by Antonio
10s
created

T_IDENTIFIER ➔ ... ➔ this.read   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 1
rs 9.2
c 1
b 0
f 0
cc 4
nc 4
nop 0
1
define(
2
    [
3
        'flagbit/JsonGenerator/Observer',
4
        'jquery'
5
    ],
6
    function(JsonGeneratorObserver, jQuery) {
7
8
        /**
9
         * @class
10
         */
11
        var JsonGeneratorRendererConstraint = function($editable, $container) {
12
13
            /**
14
             * @public
15
             * @type {JsonGeneratorObserver}
16
             */
17
            this.observer = new JsonGeneratorObserver();
18
19
            /**
20
             * @public
21
             * @param {Object} $data
22
             */
23
            this.render = function($data) {
24
25
                var $options = ['NotBlank', 'Blank', 'NotNull', 'IsNull', 'IsTrue', 'IsFalse', 'Email', 'Url', 'Ip', 'Uuid', 'Date', 'DateTime', 'Time', 'Language', 'Locale', 'Country', 'Currency', 'Luhn', 'Iban', 'Isbn', 'Issn'];
26
27
                var $dropdown = createDropdown();
28
29
                $options.forEach(function($value) {
30
31
                    var $option = document.createElement('option');
32
                    $option.value = $value;
33
                    $option.innerText = $value;
34
35
                    if($value in $data) {
36
                        $option.selected = true;
37
                    }
38
39
                    $dropdown.appendChild($option);
40
                });
41
42
                var $select2 = jQuery($dropdown).select2();
43
44
                observeChanges($select2);
45
            };
46
47
48
            /**
49
             * @public
50
             * @returns {Object}
51
             */
52
            this.read = function() {
53
54
                var $data = {};
55
56
                var $collection = $container.querySelector('select').querySelectorAll('option');
57
58
                for(var $i in $collection) {
59
                    if($collection.hasOwnProperty($i)) {
60
                        var $option = $collection[$i];
61
                        if($option.selected) {
62
                            $data[$option.value] = {};
63
                        }
64
                    }
65
                }
66
67
                return $data;
68
            };
69
70
71
            /**
72
             * @protected
73
             * @param {String} $name
74
             * @return {HTMLSelectElement}
75
             */
76
            var createDropdown = function() {
77
78
                var $dropdown = document.createElement('select');
79
                $dropdown.style.display = 'block';
80
                $dropdown.multiple = true;
81
                $container.appendChild($dropdown);
82
83
                if(!$editable) {
84
                    $dropdown.disabled = true;
85
                }
86
87
                return $dropdown;
88
            }.bind(this);
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
89
90
91
            /**
92
             * @protected
93
             * @param {HTMLSelectElement} $dropdown
94
             */
95
            var observeChanges = function($select) {
96
                $select.on('change', notify);
97
            }.bind(this);
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
98
99
100
            /**
101
             * @protected
102
             */
103
            var notify = function() {
104
105
                this.observer.notify('update');
106
            }.bind(this);
107
        };
108
109
        return JsonGeneratorRendererConstraint;
110
    }
111
);